<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="shop-info-box">
					<view class="info-item flexWrapNo">
						<view>活动名称：</view>
						<input placeholder="请输入名称" placeholder-class="placeholderclass" v-model="activity_name" maxlength="10"/>
					</view>
					<view class="info-item flexWrap">
						<view class="flexWrapNo" style="flex: 1;">
							<view>有效时间：</view>
							<picker @change="bindPickerChange" :value="index" :range="array">
								<view class="uni-input">{{array[index]}}</view>
							</picker>
						</view>
						<text class="icon-more"></text>
					</view>
					<block v-if="index==1">
						<view class="info-item flexWrap">
							<view class="flexWrapNo" style="flex: 1;">
								<view>开始时间：</view>
								<picker mode="date" :data-index='0' :value="starTime" @change="bindDateChange" :start="date">
									<view class="select-input" v-if="starTime">{{starTime}}</view>
									<view class="select-input" v-else>请选择开始时间</view>
								</picker>
							</view>
							<text class="icon-more"></text>
						</view>
						<view class="info-item flexWrap">
							<view class="flexWrapNo" style="flex: 1;">
								<view>结束时间：</view>
								<picker mode="date" :data-index='1' :value="date" @change="bindDateChange" :start="starTime">
									<view class="select-input" v-if="endTime">{{endTime}}</view>
									<view class="select-input" v-else>请选择结束时间</view>
								</picker>
							</view>
							<text class="icon-more"></text>
						</view>
					</block>
					<view class="info-item flexWrapNo">
						<view>赠送数量：</view>
						<input placeholder="请输入数量" placeholder-class="placeholderclass" type="number" v-model="sum"/>
						张/人
					</view>
					<view class="coupon-box">
						<view class="title">优惠券：</view>
						<view class="coupon-item flexWrapNo" v-if="couponId" @tap="goPage('/pages/manage/couponList?isSelect=1')">
							<view class="coupon-left flexWrapNo">
								<text class="money">{{couponInfo.price}}</text>
								<text class="money-tip">元</text>
							</view>
							<view class="coupon-line"></view>
							<view class="coupon-right flexWarpColumn">
								<text class="coupon-name">{{couponInfo.title}}</text>
								<text v-if="couponInfo.valid_timedata==1">有效期：永久</text>
								<text v-if="couponInfo.valid_timedata==2">有效期：{{couponInfo.start}} 至 {{couponInfo.end}}</text>
								<text v-if="couponInfo.valid_timedata==3">有效期：{{couponInfo.valid_configure}}天</text>
							</view>
						</view>
						<view class="select-box" v-else @tap="goPage('/pages/manage/couponList?isSelect=1')">
							<text class="select-text">+选择优惠券</text>
						</view>
					</view>
					<view class="remaks-box">
						备注：新用户首次进入店铺小程序任何页面，自动赠券。
					</view>
				</view>
				<view class="bottom-box flexWrapNo">
					<view class="define-btn" @tap="editRegisterActivityPro()">提交保存</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			const starDate = this.getDate('start')
			return {
				pageOpc: 0,
				array: ['永久有效', '自定义有效期'],
				index: 0,
				starTime: starDate,
				endTime: currentDate,
				date: currentDate,
				activity_name:"",
				couponId:"",
				couponInfo: {},
				id:"",
				register_details_id:"",
				sum:""
			}
		},
		onLoad(ops) {
			if(ops&&ops.id){
				this.id=ops.id
				this.registerActivityInfo();
			}
		},
		onReady() {
			setTimeout(() => {
				this.pageOpc = 1
			}, 100)
		},
		methods: {
			// 获取时间
			getDate(type) {
				const date = new Date();
				date.setDate(date.getDate());
				// if (type === 'start') {
				// 	date.setDate(date.getDate());
				// }
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
				if(this.index==2){
					this.valid_configure=""
				}else{
					this.valid_configure=this.couponInfo.valid_configure
				}
			},
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
			// 活动回显
			registerActivityInfo(){
				this.$request.post(this.$api.registerActivityInfo, {
					store_id:uni.getStorageSync('store_id'),
					id:this.id
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						data=data.activity_register_info
						this.activity_name=data.activity_name
						this.index=Number(data.valid_timedata)-1
						if(data.valid_timedata==2){
							let customTimeArr=data.custom_time.split(' - ')
							let start=customTimeArr[0]
							let end=customTimeArr[1]
							this.starTime=this.$tools.formatDay(start)
							this.endTime=this.$tools.formatDay(end)
						}
						if(data.register_details){
							this.register_details_id=data.register_details.id
							this.sum=data.register_details.sum
							this.couponId=data.register_details.coupon_id
							this.getCouponInfo();
						}
					}
				})
			},
			// 修改、新增活动
			editRegisterActivityPro(){
				let requestUrl=""
				if(this.id){
					requestUrl=this.$api.editRegisterActivityPro
				}else{
					requestUrl=this.$api.addRegisterActivityPro
				}
				if(!this.activity_name){
					this.$tools.showToast('名称不能为空')
					return;
				}
				if(this.$tools.containsWildcard(this.activity_name)){
					this.$tools.showToast('名称不能包含特殊字符')
					return;
				}
				if(!this.sum){
					this.$tools.showToast('请设置赠送数量')
					return;
				}
				if(this.sum<1){
					this.$tools.showToast('赠送数量未设置正确')
					return;
				}
				if(!this.couponId){
					this.$tools.showToast('请选择优惠券')
					return;
				}
				if(this.sum>this.couponInfo.remaining_quantity){
					this.$tools.showToast('赠送数量不能大于剩余数量')
					return;
				}
				this.$request.post(requestUrl, {
					store_id:uni.getStorageSync('store_id'),
					id:this.id,
					activity_name:this.activity_name,
					valid_timedata:Number(this.index)+1,
					custom_time:this.index==0?'':this.starTime+' 00:00:00 - '+this.endTime+' 23:59:59',
					sum:this.sum,
					register_details_id:this.register_details_id,
					coupon_id:this.couponId
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						this.$tools.showToast('操作成功')
						let pages = getCurrentPages(); //当前页
						let beforePage = pages[pages.length - 2]; //上个页面
						beforePage.$vm.registerActivityList()
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},2000)
					}
				})
			},
			// 获取优惠券信息
			getCouponInfo(){
				this.$request.post(this.$api.couponInfo, {
					store_id:uni.getStorageSync('store_id'),
					coupon_id:this.couponId
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					if (res.code==1) {
						if(data.valid_timedata==2){
							let customTimeArr=data.valid_configure.split(' - ')
							let start=customTimeArr[0]
							let end=customTimeArr[1]
							data.start=this.$tools.formatDay(start)
							data.end=this.$tools.formatDay(end)
						}
						this.couponInfo=data
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 0 30upx;
		.shop-info-box {
			.info-item {
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 40upx;
				color: #FFFFFF;
				padding: 30upx 0;
				border-bottom: 1upx solid #36355E;
				&:nth-last-of-type(1){
					border-bottom: none;
				}
				picker{
					flex: 1;
				}
				.select-input{
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
					flex: 1;
				}
				input {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
					flex: 1;
				}

				.icon-more {
					border-color: #FFFFFF;
				}

				.placeholderclass {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFE577;
				}
			}
			.remaks-box{
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #82819B;
				line-height: 40rpx;
				margin-top: 12upx;
			}
		}
		.coupon-box {
			margin-top: 20upx;
			.title {
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				margin-bottom: 22rpx;
			}
		
			.select-box {
				height: 140upx;
				line-height: 140upx;
				border: 1rpx dashed #FFFFFF;
				border-radius: 20upx 20upx;
				text-align: center;
		
				.select-text {
					font-size: 28rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #FD8926;
				}
			}
		
			.coupon-item {
				margin-bottom: 20upx;
				height: 140upx;
				background: #FFF5C9;
				border-radius: 20upx 20upx;
				position: relative;
		
				.left-circular {
					width: 30upx;
					height: 30upx;
					border-radius: 50%;
					background: #040336;
					position: absolute;
					left: -15upx;
					bottom: 0;
					bottom: -15rpx;
				}
		
				.right-circular {
					width: 30upx;
					height: 30upx;
					border-radius: 50%;
					background: #040336;
					position: absolute;
					right: -15upx;
					bottom: -15rpx;
				}
		
				.coupon-left {
					width: 224upx;
					align-items: baseline;
					justify-content: center;
		
					.money {
						font-size: 52upx;
						font-family: PingFang SC;
						font-weight: 500;
						line-height: 100upx;
						color: #000000;
					}
		
					.money-tip {
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #000000;
						margin-left: 20rpx;
					}
				}
		
				.coupon-line {
					width: 2upx;
					height: 110upx;
					background: #040336;
				}
		
				.coupon-right {
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 400;
					line-height: 44upx;
					color: #000000;
					margin-left: 32upx;
					flex: 1;
					.coupon-name {
						font-size: 36upx;
						font-family: PingFang SC;
						font-weight: 600;
						line-height: 50upx;
						color: #000000;
					}
				}
		
			}
		}
		.bottom-box{
			height: 120rpx;
			background: #1D1C5C;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			margin: auto;
			justify-content: center;
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			.define-btn{
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 80rpx;
				width: 322rpx;
				height: 80rpx;
				background: #26BCFD;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				text-align: center;
			}
		}
	}
</style>
